package com.example.exception;


import com.example.vo.Result;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@Slf4j
//@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
public class GlobalException {

    @ExceptionHandler(value = BizException.class)
    public Result bizException(BizException e) {
        return Result.fail(SystemErrorType.BIZ, e.getMessage());
    }

    @ExceptionHandler(value = ArithmeticException.class)
    public Result arithmeticExceptionHandler(HttpServletRequest req, ArithmeticException e)  {
        log.error("算数异常:{}", req.getRequestURI(), e.getMessage());
        return Result.fail(SystemErrorType.ARITHMETIC, e.getMessage());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    public Result nullPointerExceptionHandler(HttpServletRequest req, NullPointerException e){
        log.error("发生空指针异常！原因是:",e);
        return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID, e);
    }

    @ExceptionHandler(value = Exception.class)
    public Result exception(Exception e) {
        log.error("系统内部出现错误，错误信息：" + e.getMessage());
        e.printStackTrace();
        return Result.fail(SystemErrorType.SYSTEM_ERROR, e.getMessage());
    }
}
